import { createRouter, createWebHistory } from 'vue-router'
import Layout from '@/layout/Layout.vue'

const routes = [
  {
    path: '/',
    component: Layout,
    redirect: '/home',
    children: [
      {
        path: '/home',
        name: 'Home',
        component: () => import('@/views/Home/index.vue'),
        meta: { title: '首页' }
      },
      // 提招院校相关页面
      {
        path: '/schools/phone',
        name: 'SchoolsPhone',
        component: () => import('@/views/Schools/Phone.vue'),
        meta: { title: '院校电话' }
      },
      {
        path: '/schools/schedule',
        name: 'SchoolsSchedule',
        component: () => import('@/views/Schools/Schedule.vue'),
        meta: { title: '日程汇总' }
      },
      {
        path: '/schools/ranking',
        name: 'SchoolsRanking',
        component: () => import('@/views/Schools/Ranking.vue'),
        meta: { title: '院校排名' }
      },
      {
        path: '/schools/website',
        name: 'SchoolsWebsite',
        component: () => import('@/views/Schools/Website.vue'),
        meta: { title: '院校官网' }
      },
      {
        path: '/schools/majors',
        name: 'SchoolsMajors',
        component: () => import('@/views/Schools/Majors.vue'),
        meta: { title: '专业介绍' }
      },
      // 简章分数相关页面
      {
        path: '/brochure/outline',
        name: 'BrochureOutline',
        component: () => import('@/views/Brochure/Outline.vue'),
        meta: { title: '简章汇总' }
      },
      {
        path: '/brochure/score-lines',
        name: 'BrochureScoreLines',
        component: () => import('@/views/Brochure/ScoreLines.vue'),
        meta: { title: '历年分数' }
      },
      // {
      //   path: '/brochure/info',
      //   name: 'BrochureInfo',
      //   component: () => import('@/views/Brochure/Info.vue'),
      //   meta: { title: '简章信息' }
      // },
      {
        path: '/articles',
        name: 'Articles',
        component: () => import('@/views/Articles/index.vue'),
        meta: { title: '招生资讯' }
      },
      {
        path: '/articles/:id',
        name: 'ArticleDetail',
        component: () => import('@/views/Articles/Detail.vue'),
        meta: { title: '文章详情' }
      },
      // 招生流程相关页面
      {
        path: '/admission/registration',
        name: 'Registration',
        component: () => import('@/views/Admission/Registration.vue'),
        meta: { title: '报名入口' }
      },
      // 入围信息相关页面
      {
        path: '/admission/qualified',
        name: 'Qualified',
        component: () => import('@/views/Admission/Qualified.vue'),
        meta: { title: '入围查询' }
      },
      {
        path: '/admission/qualified/scores',
        name: 'QualifiedScores',
        component: () => import('@/views/Admission/QualifiedScores.vue'),
        meta: { title: '入围分数' }
      },
      {
        path: '/admission/qualified/info',
        name: 'QualifiedInfo',
        component: () => import('@/views/Admission/QualifiedInfo.vue'),
        meta: { title: '入围信息' }
      },
      // 缴费确认相关页面
      {
        path: '/admission/payment/links',
        name: 'PaymentLinks',
        component: () => import('@/views/Admission/PaymentLinks.vue'),
        meta: { title: '缴费链接' }
      },
      {
        path: '/admission/payment/info',
        name: 'PaymentInfo',
        component: () => import('@/views/Admission/PaymentInfo.vue'),
        meta: { title: '缴费信息' }
      },
      // 校测考纲相关页面
      {
        path: '/admission/exam/outline',
        name: 'ExamOutline',
        component: () => import('@/views/Admission/ExamOutline.vue'),
        meta: { title: '考纲与时间' }
      },
      // {
      //   path: '/admission/exam/info',
      //   name: 'ExamInfo',
      //   component: () => import('@/views/Admission/ExamInfo.vue'),
      //   meta: { title: '综测信息' }
      // },
      // 录取名单相关页面（暂未开发）
      // {
      //   path: '/admission/results/list',
      //   name: 'ResultsList',
      //   component: () => import('@/views/Admission/ResultsList.vue'),
      //   meta: { title: '录取名单' }
      // },
      // {
      //   path: '/admission/results/scores',
      //   name: 'ResultsScores',
      //   component: () => import('@/views/Admission/ResultsScores.vue'),
      //   meta: { title: '成绩查询' }
      // },
      // {
      //   path: '/admission/results/info',
      //   name: 'ResultsInfo',
      //   component: () => import('@/views/Admission/ResultsInfo.vue'),
      //   meta: { title: '录取信息' }
      // }
    ]
  },
  {
    path: '/admin',
    redirect: '/admin/dashboard'
  },
  {
    path: '/admin/login',
    name: 'AdminLogin',
    component: () => import('@/views/Admin/Login.vue'),
    meta: { title: '管理员登录' }
  },
  {
    path: '/admin',
    component: () => import('@/layout/AdminLayout.vue'),
    meta: { requiresAuth: true },
    children: [
      {
        path: '/admin/dashboard',
        name: 'AdminDashboard',
        component: () => import('@/views/Admin/Dashboard.vue'),
        meta: { title: '控制台', requiresAuth: true }
      },
      {
        path: '/admin/articles',
        name: 'AdminArticles',
        component: () => import('@/views/Admin/Articles.vue'),
        meta: { title: '文章管理', requiresAuth: true }
      },
      {
        path: '/admin/categories',
        name: 'AdminCategories',
        component: () => import('@/views/Admin/Categories.vue'),
        meta: { title: '分类管理', requiresAuth: true }
      },
      {
        path: '/admin/schools',
        name: 'AdminSchools',
        component: () => import('@/views/Admin/Schools.vue'),
        meta: { title: '院校管理', requiresAuth: true }
      },
      {
        path: '/admin/majors',
        name: 'AdminMajors',
        component: () => import('@/views/Admin/Majors.vue'),
        meta: { title: '专业管理', requiresAuth: true }
      },
      {
        path: '/admin/score-lines',
        name: 'AdminScoreLines',
        component: () => import('@/views/Admin/ScoreLines.vue'),
        meta: { title: '分数线管理', requiresAuth: true }
      },
      {
        path: '/admin/system-config',
        name: 'AdminSystemConfig',
        component: () => import('@/views/Admin/SystemConfig.vue'),
        meta: { title: '系统配置', requiresAuth: true }
      },
      {
        path: '/admin/file-upload',
        name: 'AdminFileUpload',
        component: () => import('@/views/Admin/FileUpload.vue'),
        meta: { title: '文件管理', requiresAuth: true }
      },
      {
        path: '/admin/applications',
        name: 'AdminApplications',
        component: () => import('@/views/Admin/Applications.vue'),
        meta: { title: '报名管理', requiresAuth: true }
      },
      {
        path: '/admin/qualification',
        name: 'AdminQualification',
        component: () => import('@/views/Admin/Qualification.vue'),
        meta: { title: '入围管理', requiresAuth: true }
      },
      {
        path: '/admin/payment',
        name: 'AdminPayment',
        component: () => import('@/views/Admin/Payment.vue'),
        meta: { title: '缴费管理', requiresAuth: true }
      },
      {
        path: '/admin/exam-outlines',
        name: 'AdminExamOutlines',
        component: () => import('@/views/Admin/ExamOutlines.vue'),
        meta: { title: '考纲管理', requiresAuth: true }
      },
      {
        path: '/admin/admission',
        name: 'AdminAdmission',
        component: () => import('@/views/Admin/Admission.vue'),
        meta: { title: '录取管理', requiresAuth: true }
      }
    ]
  },
  {
    path: '/404',
    name: 'NotFound',
    component: () => import('@/views/404.vue'),
    meta: { title: '页面不存在' }
  },
  {
    path: '/:pathMatch(.*)*',
    redirect: '/404'
  }
]

const router = createRouter({
  history: createWebHistory(),
  routes,
  scrollBehavior() {
    return { top: 0 }
  }
})

// 路由守卫
router.beforeEach(async (to, from, next) => {
  // 设置页面标题
  if (to.meta.title) {
    if (to.path.startsWith('/admin')) {
      document.title = `${to.meta.title} - 管理系统`
    } else {
      document.title = `${to.meta.title} - 浙江高职提前招生网`
    }
  }
  
  // 检查是否需要认证
  if (to.meta.requiresAuth) {
    // 动态导入store
    const { useAdminStore } = await import('@/stores/admin')
    const adminStore = useAdminStore()
    
    // 初始化用户信息
    adminStore.initUserInfo()
    
    // 检查是否已登录
    if (!adminStore.isLoggedIn) {
      next('/admin/login')
      return
    }
    
    // 验证token是否有效
    const isValid = await adminStore.validateAccessToken()
    if (!isValid) {
      // token无效，尝试刷新
      try {
        await adminStore.refreshAccessToken()
      } catch (error) {
        // 刷新失败，跳转到登录页
        next('/admin/login')
        return
      }
    }
  }
  
  next()
})

export default router